from datetime import datetime
from typing import List

import pandas as pd

from app.models.po.hanfan.hanfan_new_predict_po import HanfanNewPredictPO
from app.models.po.luzha.luzha_predict_po import LuzhaPredictPO
from app.services.business import hanfan_biz_service
from app.services.business.hanfan import luzha_biz_service
from app.services.chart.Line import Line
from app.utils import date_util, simple_util, string_util
from datetime import timedelta


class HanfanVComponent(Line):
    """
    钒含量数据表
    """

    def generate(self, start, end):

        if string_util.is_empty(start) or string_util.is_empty(end):
            base = date_util.get_start_of_hour(datetime.now())
            start = base - timedelta(days=1)
            end = base

        yAxis_array = [
            {
                "name": "钒含量曲线",
                "type": 'value'
            }
        ]

        records: List[HanfanNewPredictPO] = hanfan_biz_service.get_predict_data(start, end)
        data_x = []
        data_y_array = [dict(name="", y=[], yAxisIndex=0,type="line"),
                        dict(name="", y=[""], yAxisIndex=0,type="line")]
        for record in records:
            data_x.append(date_util.date_time_to_str(record.DATE_TIME, "%H:%M"))
            data_y_array[0]['name'] = "当前值"
            data_y_array[0]['y'].append(record.CG_LT_GL_GL04_Tie_V)
            data_y_array[0]['type'] = "line"

            data_y_array[1]['name'] = "预测值"
            data_y_array[1]['y'].append(record.CG_LT_GL_GL04_Tie_V_PREDICT)
            data_y_array[1]['type'] = "line"

        if len(records) > 0:
            data_x.append(date_util.date_time_to_str(records[len(records) - 1].DATE_TIME + timedelta(hours=1), "%H:%M"))

        return dict(x=data_x,
                    tooltip={
                        "trigger": 'axis'
                    },
                    y_array=data_y_array, desc="钒含量变化曲线", multiSerie=True,
                    yAxis_array=yAxis_array)
